package com.hyg.lib_common.Net;

import android.content.Context;
import android.util.Log;

import com.hyg.lib_common.DataUtil.UserSPUtils;
import com.hyg.lib_common.constant.UrlUtils;

import java.util.ArrayList;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author: Administrator
 * @date: 2021/3/31
 */
public class CommonHttpUtil {


    public static void getHttp(String url, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .addHeader("User-Agent", "PostmanRuntime/7.26.8")
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    public static void postHttp(String js, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("data", js)
                .build();
        Request request = new Request.Builder()
                .url("http://robotuser.system.itcrm.com/Iface")
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback
        );
    }


    /**
     * description: 一键登录
     *
     * @time 2021/4/16 10:52
     */
    public static void loginByPhone(String token, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("alitoken", token)
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.oneKeyloginAddr)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback
        );
    }


    /**
     * description: 获取验证码
     */
    public static void getCode(String phone, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("phone", phone)
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.codeaddr)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    /**
     * description: 验证码登录
     *
     * @time 2021/4/16 10:51
     */
    public static void loginByCode(String phone, String code, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("phone", phone)
                .add("smsCode", code)
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.loginPhoneAddr)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * description: 用户名 密码登录
     */
    public static void loginByPassword(String phone, String password, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("phone", phone)
                .add("password", password)
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.loginPwdAddr)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * description:  获取个人音乐收藏列表
     */
    public static void getFavList(Context context, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.LIST_FAVORITE)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * description:  删除（多个）个人音乐收藏列表
     */
    public static void removeMultilFavList(Context context, String data, Callback callback) {
        Log.i("lgb", "removeMultilFavList: " + data);
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("musicIds", data)
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.REMOVE_FAVORITE_MULTI)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * description:  增加（多个）个人音乐收藏列表
     */
    public static void addMultilFavList(Context context, String data, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("musicIds", data)
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.ADD_FAVORITE_MULTI)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    /**
     * description: 获取音乐分类音乐列表
     *
     * @param id 文章id
     */
    public static void getMusicCategoryData(String id, Callback callback) {
        String url = UrlUtils.NEW_CATEGORY_LIST_Three + "?categoryId=" + id;
        getHttp(url, callback);
    }


  /**
     * description: 获取音乐分类音乐列表
     *
     * @param id 文章id
     */
    public static void getMusicCategoryThreeData(Context context,String id, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("categoryId", id)
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.NEW_CATEGORY_LIST_Three)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    /**
     * description: 获取音乐分类音乐列表
     *
     * @param
     */
    public static void getMusicCategoryData(Callback callback) {
        String url = UrlUtils.NEW_CATEGORY_LIST_Two;
        getHttp(url, callback);
    }


    /**
     * description: 获取音乐分类最低级音乐列表
     *
     * @param id 文章id
     */
    public static void getMusicListData(Context context, String id, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("categoryId", id)
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.MUSIC_LIST)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * description: 获取音乐知识文章列表
     */
    public static void getKnowLedgeCategoryData(int id, Callback callback) {
        String url = UrlUtils.CATE_LIST + "?categoryId=" + id;
        getHttp(url, callback);
    }


    /**
     * description: 获取音乐知识文章详情
     *
     * @param id 文章id
     */
    public static void getKnowLedgeArticleData(int id, Callback callback) {
        String url = UrlUtils.DOC_LIST + "?categoryId=" + id;
        getHttp(url, callback);
    }


    /**
     * description:  banner 图片数据
     *
     * @param
     */
    public static void getViewpagerData(Callback callback) {
        String url = UrlUtils.BANNER_LIST;
        getHttp(url, callback);
    }

    /**
     * description:  获取banner文章详情
     *
     * @param id banner文章id
     */
    public static void getViewpagerDetail(String id, Callback callback) {
        String url = UrlUtils.BANNER_DETAIL + "?id=" + id;
        getHttp(url, callback);
    }

    /**
     * description:  获取24节气data
     */
    public static void getSolarTermDetail(String id, Callback callback) {
        String url = UrlUtils.SOLAR_TERM;
        getHttp(url, callback);
    }

    public static void loginWechat(String openid, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("wxUid", openid)
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.LOGIN_WECHAT)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    /**
     * description: 微信绑定手机
     *
     * @param openid 微信openid
     * @param code   验证码
     */
    public static void bindPhone(String phone, String openid, String code, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("smsCode", code)
                .add("phone", phone)
                .add("wxUid", openid)
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.BIND_PHONE)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    /**
     * description:
     *
     * @param type           1：养生，2：助眠，3：解压
     * @param sleepMusicType 0：音乐疗愈类型非助眠，1：15分钟，2：20分钟，3：40分钟
     */
    public static void getHealthMusic(Context context, int type, int sleepMusicType, Callback callback) {
        Log.i("lgb", "getHealthMusic: " + UserSPUtils.getToken(context));
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("type", type + "")
                .add("sleepMusicType", sleepMusicType + "")
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.Recommended_MUSIC)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    /**
     * description: 换一换音乐列表中某几首歌
     *
     * @param type           1：养生，2：助眠，3：解压
     * @param sleepMusicType 0：音乐疗愈类型非助眠，1：15分钟，2：20分钟，3：40分钟
     */
    public static void changeHealthMusic(Context context, ArrayList<String> list, int type, int sleepMusicType, Callback callback) {
        Log.i("lgb", "getHealthMusic: " + UserSPUtils.getToken(context));
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("changeMusicIds", list.toString())
                .add("type", type + "")
                .add("sleepMusicType", sleepMusicType + "")
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.Change_Recommended_MUSIC)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    /**
     * description: 提交音乐模块自测问卷结果
     *
     * @param type   1：养生，2：助眠，3：解压
     * @param result 问卷结果
     */
    public static void saveMusicTestResult(Context context, int type, String result, Callback callback) {
        Log.i("lgb", "getHealthMusic: " + UserSPUtils.getToken(context));
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("type", type + "")
                .add("result", result)
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.Testting_Result_Save)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * description: 提交音乐模块自测问卷结果
     *
     * @param type 1：养生，2：助眠，3：解压
     */
    public static void getMusicTestResult(Context context, int type, Callback callback) {
        Log.i("lgb", "getHealthMusic: " + UserSPUtils.getToken(context));
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("type", type + "")
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.Testting_Result_Get)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    public static void getMusicCourceInfo(Context context, int type, Callback callback) {
        Log.i("lgb", "getHealthMusic: " + UserSPUtils.getToken(context));
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("type", type + "")
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.Music_Cource_Info)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    public static void updateCourceTimes(Context context, int times, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("type", "1")
                .add("times", times + "")
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.Update_Cource_times)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    public static void updateCourceResultInfo(Context context, int feeling, String advice, String adviceDetail, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("type", "1")
                .add("feeling", feeling + "")
                .add("advice", advice)
                .add("adviceDetail", adviceDetail)
                .add("token", UserSPUtils.getToken(context))
                .build();
        Request request = new Request.Builder()
                .url(UrlUtils.Update_Cource_times)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }


}
